package com.young.rabbitmq.controller;

import com.young.rabbitmq.config.BaseExcahngeQueueConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendMsgController {
    //注入RabbitMQ的模板
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送简单消息
     */
    @GetMapping("/send/simple")
    public String sendMsg(@RequestParam String msg, @RequestParam String key){
        /**
         * 发送消息
         * 参数一：交换机名称
         * 参数二：路由key
         * 参数三：发送的消息
         */
        rabbitTemplate.convertAndSend(BaseExcahngeQueueConfig.ITEM_TOPIC_EXCHANGE ,key ,msg);
        //返回消息
        return "发送消息成功！";
    }


    @RequestMapping("/send/ttl")
    public String sendTtlMsg(){
        rabbitTemplate.convertAndSend("TTL_EXCHANGE","TTL","过期消息");
        return "发送成功";
    }

    @RequestMapping("/send/dlx")
    public String sendDlxMsg(){
        rabbitTemplate.convertAndSend("allow_dlx_change","dlx_routingkey","死信队列消息");
        return "发送成功";
    }


    @RequestMapping("/send/tx")
    public String sendTxMsg(){

        rabbitTemplate.convertAndSend("TTL_EXCHANGE","TTL","过期消息");
        return "发送成功";
    }
}
